//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "stdafx.h"
#include "GeneralProperties.hpp"
#include "ioctl.h"
#include "regmgr.h"
#include "commonresource.h"
#include "CheckMACs.h"
#include "ClearSystemSettings.h"
#include "..\WBTTSCMN\etheraddr.hpp"


const TCHAR c_szRegTerminalName[] = TEXT("Ident");
const TCHAR c_szRegTerminalNameKey[] = TEXT("Name");

CGeneralPropPage::CGeneralPropPage (CTerminalPropSheet *pOwner) : CTerminalPropPage(pOwner)
{
	DEBUGMSG (1, (L"CGeneralPropPage::CGeneralPropPage\n"));
	
	Init (IDD_GENERAL_PAGE);

	m_osVersion[0] = 0;
	m_oemVersion [0] = 0;
	m_TerminalName[0] = 0;
}

CGeneralPropPage::~CGeneralPropPage()
{
}

LRESULT CGeneralPropPage::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	
	DEBUGMSG (1, (L"CGeneralPropPage::OnInitDialog, class is [%s]\n", GetId()));
	RETAILMSG (1,(L"CGeneralPropPage::OnInitDialog()-- TerminalName:%s",
		m_TerminalName));
	CTerminalPropPage::OnInitDialog (uMsg, wParam, lParam, bHandled);
	
	m_resetDefaultsCheck.Attach (GetDlgItem(IDC_DEF_RESET_CHK));
	m_dispOsVersion.Attach (GetDlgItem(IDC_STATIC_WINCE_VER));
	m_dispOemVersion.Attach (GetDlgItem(IDC_STATIC_OEM_VER));
	//changed on 16th may 01 by vij
	m_dispOemProductID.Attach(GetDlgItem(IDC_STATIC_PRODID));
	m_wndTerminalName.Attach (GetDlgItem (IDC_TERMINAL_NAME));
	m_wndTerminalName.SendMessage(EM_SETLIMITTEXT,MAXLEN_TERMINAL_NAME-1,0);
	ImmAssociateContext((HWND)m_wndTerminalName, NULL);

	//AAS
	//===========================================================================
	TCHAR * macAddr = GetFormatedMacAddress();
	if ( macAddr )
	{
	    m_MACAddress.Attach(GetDlgItem (IDC_STATIC_MAC_ADDRESS));
	    m_MACAddress.SetWindowText (macAddr);

	    free(macAddr);
	}

    char ver[20];
    TCHAR wver[100];
	TCHAR full_wver[100] = TEXT("Version ");

	GetMACVersion(ver, TEXT("\\Windows\\macs.dat"));

	mbstowcs(wver,ver,50);
	wcscat(full_wver, wver);
	//wcscat(wver, L" Release");
	
	m_Version.Attach(GetDlgItem(IDC_STATIC_VERSION));
	m_Version.SetWindowText (full_wver);
	//===========================================================================

	GetOsInfo();
	UpdateOsInfo();
	UpdateOemInfo();
	
	if( GetProductID() )
		UpdateProductID();
	
	//UpdateTerminalName();
	m_wndTerminalName.SetWindowText(m_TerminalName);

	return 1;
}

void CGeneralPropPage::UpdateTerminalName()
{
	RETAILMSG (1,(L"CGeneralPropPage::UpdateTerminalName()-- TerminalName:%s",
		m_TerminalName));
	m_wndTerminalName.SetWindowText(m_TerminalName);
}

void CGeneralPropPage::GetOsInfo()
{
	RETAILMSG (1, (L"CGeneralPropPage::GetOsInfo() -- TerminalName: %s\n",
			m_TerminalName));
	OSVERSIONINFO osVer;

	memset(&osVer,0,sizeof(OSVERSIONINFO));
	// OEM defined fn 
    //GetUUID();
	
	osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	int ret = GetVersionEx(&osVer);
      	//AAS  _stprintf (m_osVersion, _T("%2u.%2u"), osVer.dwMajorVersion, osVer.dwMinorVersion);
	_stprintf (m_osVersion, _T("%2u.%2u R3"), osVer.dwMajorVersion, osVer.dwMinorVersion);
}

//
BOOL CGeneralPropPage::GetProductID()
{
	RETAILMSG (1, (L"CGeneralPropPage::GetProductID() -- TerminalName: %s\n",
			m_TerminalName));
	GUID myUUID;
	BOOL bRetVal = FALSE;
	
	bRetVal = GetUUID(&myUUID);
	if(!bRetVal)
	{
		return FALSE;
	}
	
	wsprintf(m_oemProductID,L"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\r\n",
		myUUID.Data1,
		myUUID.Data2,
		myUUID.Data3,
		myUUID.Data4[0],
		myUUID.Data4[1],
		myUUID.Data4[2],
		myUUID.Data4[3],
		myUUID.Data4[4],
		myUUID.Data4[5],
		myUUID.Data4[6],
		myUUID.Data4[7]	
		);

	
	return TRUE;
}


LRESULT CGeneralPropPage::OnResetCheck (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled)
{
    // get check state
	RETAILMSG (1, (L"CGeneralPropPage::OnResetCheck() -- TerminalName: %s\n",
			m_TerminalName));
	int state  = Button_GetCheck (m_resetDefaultsCheck);
	m_bSetDefaults = (state == BST_CHECKED);
	DEBUGMSG (1, (L"CGeneralPropPage::OnResetCheck, state %d, m_bSetDefaults %d\n", state, m_bSetDefaults));
	
	if (m_bSetDefaults)
	{
		DEBUGMSG (1, (L"displaying dlg\n"));

		LPCTSTR pszTitle = LoadStringEx (IDS_RESET_CAPTION);
		LPCTSTR pszMsg   = LoadStringEx (IDS_RESET_CONFIRM);

		if ((pszTitle) && (pszMsg) && (MessageBox (pszMsg, pszTitle, MB_ICONQUESTION | MB_YESNO) == IDYES))
		{
			DEBUGMSG (1, (L"dlg sez ok\n"));
			GetOwner().SetDefaults (TRUE);
			GetOwner().SetReboot (TRUE);
		}
		else
		{
			DEBUGMSG (1, (L"dlg sez not ok\n"));
	        m_bSetDefaults = FALSE;

			// unset the check box
			Button_SetCheck (m_resetDefaultsCheck, BST_UNCHECKED);
		}
	}
	return 1;
}

LRESULT CGeneralPropPage::OnSetActive (int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	if (ReadRegistrySettings ())
		UpdateTerminalName ();

	return 0;
}

BOOL CGeneralPropPage::ReadRegistrySettings()
{
	// read terminal name from registry
	HKEY hkey = OpenRegistryKey (HKEY_LOCAL_MACHINE, c_szRegTerminalName, KEY_READ);
	if (hkey != NULL)
	{
		// get the details
		LPCTSTR pszStr = NULL;
		if (NULL != (pszStr = GetRegistryKeyString (hkey, c_szRegTerminalNameKey)))
		{
			// copy it into our variable
			_tcscpy (m_TerminalName, pszStr);
			if (pszStr != NULL)
		   		free ((LPVOID)pszStr);
		}
		RETAILMSG (1, (L"CGeneralPropPage::ReadRegistrySettings() -- TerminalName: %s\n",
			m_TerminalName));
		CloseRegistryKey (hkey);
	}
	else
		RETAILMSG (1, (L"!!! OpenRegistryKey failed !!!\n"));
	return TRUE;
}

BOOL CGeneralPropPage::WriteRegistrySettings()
{
    // write terminal name to the registry
	HKEY hkey;
	DWORD dwDisposition = 0;

	RETAILMSG(1, (L"CGeneralPropPage::WriteRegistrySettings()-->Writing to Registry TerminalName\n"));
	if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, c_szRegTerminalName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, 
						&dwDisposition) == ERROR_SUCCESS)
	{
		// write the friendly name
		SetRegistryKeyString (hkey, c_szRegTerminalNameKey, m_TerminalName);
		RegCloseKey (hkey);
	}
	else
		RETAILMSG(1, (L"!!! RegCreateKeyEx failed !!!\n"));
	
	if (m_bSetDefaults)
	{
        DWORD state = SETUP_WIZARD_ENABLED;
        SetSetupWizardRunState(state);
		
		//AAS
		MoveSystemSettings();
    }

	return TRUE;
}


void CGeneralPropPage::UpdateOsInfo()
{
	if ( m_osVersion != NULL)
		m_dispOsVersion.SetWindowText (m_osVersion);		
}


void CGeneralPropPage::UpdateOemInfo()
{
	if ( m_oemVersion != NULL)	
		m_dispOemVersion.SetWindowText (m_oemVersion);		
}

void CGeneralPropPage::UpdateProductID()
{
	if ( m_oemProductID != NULL)	
		m_dispOemProductID.SetWindowText (m_oemProductID);		
}


BOOL CGeneralPropPage::StoreData()
{
	return TRUE;
}

BOOL CGeneralPropPage::Validate()
{
	TCHAR szDesc[MAX_DISPLAY_STRING] = {0};
	TCHAR szTitle[MAX_DISPLAY_STRING] = {0}; 
	
	HWND hWndLeftFocus;
	BOOL bInvalidChar = FALSE;
	
	if (m_wndTerminalName.GetWindowTextLength() == 0)
	{
		LoadString (ghInstance, IDS_INVALID_TERMINAL_NAME_TITLE, szTitle, MAX_DISPLAY_STRING);
		LoadString (ghInstance, IDS_INVALID_TERMINAL_NAME_NULL_MESSAGE, szDesc, MAX_DISPLAY_STRING);
		MessageBox(szDesc, szTitle, MB_OK);
		hWndLeftFocus = m_wndTerminalName.SetFocus();
		return FALSE;
	}

	RETAILMSG (1, (L"CGeneralPropPage::Validate() Entered\n"));
	m_wndTerminalName.GetWindowText (m_TerminalName, MAXLEN_TERMINAL_NAME);
	int nLen = _tcslen(m_TerminalName);
	size_t idx;

	// Make sure that all the characters in the terminal name are valid.
	if (!(m_TerminalName[0] >= _T('A') && m_TerminalName[0] <= _T('Z')) &&
		!(m_TerminalName[0] >= _T('a') && m_TerminalName[0] <= _T('z')))
	{
		bInvalidChar = TRUE;
		goto ERR_RET;
	}

	ASSERT(nLen > 0);
	if ((m_TerminalName[nLen-1] == L'-') || (m_TerminalName[nLen-1] == L'_'))
	{
		bInvalidChar = TRUE;
		goto ERR_RET;
	}

	idx = _tcscspn (m_TerminalName, _T(" \"<>*+=\\|?:;~$%^{}@()[]/\'#.,&"));
	if (idx >= 0 && idx < _tcslen (m_TerminalName))
		bInvalidChar = TRUE;

ERR_RET:

	if (bInvalidChar)
	{
		TCHAR szMessage [MAX_DISPLAY_STRING] = {0};
		RETAILMSG (1, (L"CNetworkPropPage::Validate() NULL Checking of Terminal Name \n"));
		
		LoadString (ghInstance, IDS_INVALID_TERMINAL_NAME_TITLE, szTitle,MAX_DISPLAY_STRING);
		LoadString (ghInstance, IDS_INVALID_TERMINAL_NAME_MESSAGE, szMessage,MAX_DISPLAY_STRING);

		MessageBox (szMessage, szTitle, MB_ICONINFORMATION | MB_OK);
		m_wndTerminalName.SendMessage (EM_SETSEL, 0, -1);
		m_wndTerminalName.SetFocus();
		return FALSE;			
	}

	return TRUE;
}
